package bjtu.cit.bookshop.controller.admin;

import bjtu.cit.bookshop.common.ErrorCode;
import bjtu.cit.bookshop.common.Md5;
import bjtu.cit.bookshop.controller.BaseController;
import bjtu.cit.bookshop.entities.Employee;
import bjtu.cit.bookshop.entities.Publisher;
import bjtu.cit.bookshop.service.impl.PublisherService;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jia19 on 2015/10/12.
 */
@Controller
@RequestMapping("/admin/")
public class PublisherController extends BaseController {
    @Resource
    private PublisherService publisherService = null;

    @RequestMapping("publisher.do")
    public String publisher(HttpSession session, ModelMap model) {
        String loginName = checkAdminLogin(session);
        if (loginName == null) {
            return "/admin/index";
        }
        model.addAttribute("isLogin", true);
        model.addAttribute("loginName", loginName);
        model.addAttribute("current", "publisher");

        List<Publisher> publisherList = publisherService.getPublisherList();
        if (publisherList == null) {
            publisherList = new ArrayList<Publisher>();
        }
        model.addAttribute("publisherList", publisherList);

        return "admin/publisher";
    }

    @RequestMapping("delPublisher.do")
    @ResponseBody
    public ErrorCode delPublisher(HttpServletRequest request, HttpSession session) {
        if (checkAdminLogin(session) == null) {
            return NO_AUTH;
        }
        String publisherId = request.getParameter("id");
        if (publisherId == null) {
            return PARA_ERROR;
        }
        try {
            this.publisherService.deletePublisherById(Integer.valueOf(publisherId));
        } catch (Exception e) {
            e.printStackTrace();
            return PARA_ERROR;
        }
        return SUCCESS;
    }


    @RequestMapping("newPublisher.do")
    public String newPublisher(HttpSession session, ModelMap model) {
        String loginName = checkAdminLogin(session);
        if (loginName == null) {
            return "/admin/index";
        }
        model.addAttribute("isLogin", true);
        model.addAttribute("loginName", loginName);
        model.addAttribute("current", "publisher");

        return "/admin/newPublisher";
    }

    @RequestMapping("addPublisher.do")
    @ResponseBody
    public ErrorCode addPublisher(HttpSession session, HttpServletRequest request) {
        if (checkAdminLogin(session) == null) {
            return NO_AUTH;
        }

        String name = request.getParameter("name");
        String linkname = request.getParameter("linkname");
        String address = request.getParameter("address");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        String website = request.getParameter("website");

        Publisher publisher = new Publisher();
        try {
            publisher.setName(name);
            publisher.setLinkman(linkname);
            publisher.setAddress(address);
            publisher.setEmail(email);
            publisher.setPhone(phone);
            publisher.setWebsite(website);
        } catch (Exception e) {
            e.printStackTrace();
            return PARA_ERROR;
        }
        publisherService.createAPublisher(publisher);
        return SUCCESS;
    }

    @RequestMapping("publisherEdit.do")
    public String publisherEdit(HttpSession session, HttpServletRequest request, ModelMap model) {
        String loginName = checkAdminLogin(session);
        if (loginName == null) {
            return "/admin/index";
        }
        model.addAttribute("isLogin", true);
        model.addAttribute("loginName", loginName);
        model.addAttribute("current", "publisher");

        String id = request.getParameter("id");
        if (id == null) {
            return "admin/badParameter";
        }
        try {
            Publisher publisher = publisherService.getPublisherById(Integer.valueOf(id));
            model.addAttribute("publisher", publisher);
        } catch (Exception e) {
            e.printStackTrace();
            return "admin/badParameter";
        }

        return "admin/publisherEdit";
    }

    @RequestMapping("publisherSave.do")
    @ResponseBody
    public ErrorCode publisherSave(HttpSession session, HttpServletRequest request) {
        if (checkAdminLogin(session) == null) {
            return NO_AUTH;
        }

        String id = request.getParameter("publisherId");
        String name = request.getParameter("name");
        String linkman = request.getParameter("linkman");
        String address = request.getParameter("address");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");

        Publisher publisher = new Publisher();
        try {
            publisher.setId(Integer.valueOf(id));
            publisher.setName(name);
            publisher.setLinkman(linkman);
            publisher.setAddress(address);
            publisher.setEmail(email);
            publisher.setPhone(phone);

        } catch (Exception e) {
            e.printStackTrace();
            return PARA_ERROR;
        }
        publisherService.updatePublisherInfo(publisher);
        return SUCCESS;

    }

}
